#/usr/bin/perl
use strict;

use Data::Dumper;
use File::Slurp;
use Ball;
use interaction;
use file_IO;
use UI;
use subs qw(output);

%::game; #we can use this to store game settings - or do you think those shoudl go to data as well?

my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml", forcearray=>['character']);#open_data_file($ARGV[0]); #I suppose this is static data only
my $all_data = $xml->XMLin("inter.xml", forcearray=>['phrase','state','transition'], keyattr=>['name']); #and this maybe dynamic data

$::game{debug}=0;
$ARGV[0]=~/-d/ and $::game{debug}=1;


my $wojak = new Ball(0,0,["plunger","kippah","axe","Camembert","ion","Hitler's Left Ball in a Jar"]);

my %actions = ( "w" => sub { $wojak->{y}++ if ( $wojak->can_go($data,0,1) ); } ,
                "s" => sub { $wojak->{y}-- if ( $wojak->can_go($data,0,-1) ); } ,
                "a" => sub { $wojak->{x}-- if ( $wojak->can_go($data,-1,0) ); } ,
                "d" => sub { $wojak->{x}++ if ( $wojak->can_go($data,1,0) ); } ,
                "i" => sub { $wojak->print_inv(); } ,
                "m" => sub { $wojak->show_map($data); } ,
                "q" => sub { exit(); } ,
                "l" => sub { $wojak->look($data); } ,
                "t" => sub { talk($wojak,$data); } , #talk is in interaction.pm
                "u" => sub { interaction("Yourself", $wojak); } , #this too
                "save" => sub { save("saveinter.xml", $all_data); $wojak->saveme();} ,
                "load" => sub { $all_data=load("saveinter.xml"); $wojak->loadme();} ,
                "dump" => sub { open(LOG,'>'."dump.txt");print LOG Dumper($all_data); },
                "slurpdump" => sub {  write_file 'mydump.log', Dumper( $all_data ); }

                 );
THE_GAME();



sub THE_GAME()
{
	my $here = $wojak->get_loc($data);
	output; output; output;
	output "----------------------------------------------------------------------------";
	output "----------------------------------------------------------------------------";
	output; output; output;
    output ("You're in $here. ",1);

	output ($data->{region}->{$here}->{description}, 1) if $data->{region}->{$here}->{description};
	output("I couldn't come up with a good idea for this country. I am not a clever man. If you can think of anything interesting, tell us.",1) if $data->{region}->{$here}->{dev};
	
    print_characters($here) if $here ne 'Unknown'; #This prevents the Unknown problem
    output "What do? >", 2;
	my $in = &input;
	chomp($in);
	my $commandfound=0;
        $in =~ /^go to ([\w\s\.]*)/ and $wojak->go($all_data, $here, $1) and $commandfound=1;
        $in =~ /^talk to ([\w\s\.]*)/ and $wojak->talkto($all_data, $here, $1) and $commandfound=1;
	if ($actions{$in}) {&{$actions{$in}};$commandfound=1} 
	$commandfound or output "wut o_O",1;

	THE_GAME(); #the game never stops
}